﻿namespace Manhua3.Helper
{
    using System;
    using System.Threading.Tasks;
    using Windows.Graphics.Imaging;
    using Windows.Storage.Streams;
    using Windows.UI.Xaml.Media.Imaging;
    using WinRTXamlToolkit.Imaging;

    public static class ImageHelper
    {
        public async static Task<WriteableBitmap> GetHalfImageAsync(
            this IRandomAccessStream stream,
            bool left)
        {
            stream.Seek(0);

            // 创建图片解码器
            var decoder = await BitmapDecoder.CreateAsync(stream);

            // 初始化宽度 高度
            var width = (int)decoder.PixelWidth;
            var height = (int)decoder.PixelHeight;
            var halfWidth = width / 2;

            // 创建可读写的Bitmap图片
            var bmp = new WriteableBitmap(halfWidth + 1, height + 1);

            // 获取原始图片像素颜色， 以及对应图片像素颜色
            var source = (await decoder.GetPixelDataAsync()).DetachPixelData().ToPixels();
            var target = bmp.PixelBuffer.GetPixels();

            await Task.Factory.StartNew(() =>
            {
                if (left)
                {
                    // 将原始图片左半部图片颜色 拷贝到对应图片
                    for (int i = 0; i < height; i++)
                    {
                        for (int j = 0; j < halfWidth; j++)
                        {
                            target[i * halfWidth + j] = source[i * width + j];
                        }
                    }
                }
                else
                {
                    // 将原始图片右半部图片颜色 拷贝到对应图片
                    for (int i = 0; i < height; i++)
                    {
                        for (int j = halfWidth; j < width; j++)
                        {
                            target[i * halfWidth + j - halfWidth] = source[i * width + j];
                        }
                    }
                }
            });

            return bmp;
        }

        public static async Task<WriteableBitmap> ContactImageAsync(
            this IRandomAccessStream left,
            IRandomAccessStream right)
        {
            left.Seek(0);
            right.Seek(0);

            // 创建图片解码器
            var decoderLeft = await BitmapDecoder.CreateAsync(left);
            var decoderRight = await BitmapDecoder.CreateAsync(right);

            // 初始化宽度 高度
            var leftWidth = (int)decoderLeft.PixelWidth;
            var leftHeight = (int)decoderLeft.PixelHeight;
            var rightWidth = (int)decoderRight.PixelWidth;
            var rightHeight = (int)decoderRight.PixelHeight;
            var width = leftWidth + rightWidth;
            var height = (int)Math.Max(leftHeight, rightHeight);

            // 创建可读写的Bitmap图片
            var bmp = new WriteableBitmap(width, height);

            // 获取原始图片像素颜色， 以及对应图片像素颜色
            var target = bmp.PixelBuffer.GetPixels();
            var sourceLeft = (await decoderLeft.GetPixelDataAsync()).DetachPixelData().ToPixels();
            var sourceRight = (await decoderRight.GetPixelDataAsync()).DetachPixelData().ToPixels();

            await Task.Factory.StartNew(() =>
            {
                // 拷贝左图
                for (int i = 0; i < leftHeight; i++)
                {
                    for (int j = 0; j < leftWidth; j++)
                    {
                        target[i * width + j] = sourceLeft[i * leftWidth + j];
                    }
                }

                // 拷贝右图
                int offset = leftWidth;
                for (int i = 0; i < rightHeight; i++)
                {
                    for (int j = 0; j < rightWidth; j++)
                    {
                        target[i * width + j + offset] = sourceRight[i * rightWidth + j];
                    }
                }
            });

            return bmp;
        }

        public async static Task<bool> CheckImageCanHalf(this IRandomAccessStream stream)
        {
            var decoder = await BitmapDecoder.CreateAsync(stream);
            return (double)decoder.PixelWidth / (double)decoder.PixelHeight > 1.3 ? true : false;
        }

        public async static Task<BitmapImage> GetBitmapAsync(this IRandomAccessStream stream)
        {
            stream.Seek(0);
            var bmp = new BitmapImage();
            await bmp.SetSourceAsync(stream);
            return bmp;
        }
    }
}
